/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-30 14:17:09
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-30 14:54:15
 */
// js继承的方式6种基础的，加Object那种和class语法糖那种共计是8种
// 1、原型链继承
function SuperType(){
    this.type=type;
}
SuperType.prototype.getSuperValue = function(){
    return this.type;
}
function SupType(){
    this.age = age;
}
SubType.prototype = new SuperType();
SubType.prototype.getSubType = function(){
    return this.age;
}
var instance = new SubType();
alert(instance.getSuperValue());

// 优点是能快速使用原型链进行继承功能实现，副作用是链上没有隔离，修改一处其他地方也会变化

// 2、原型式继承
function object(o){
    function F(){
        F.prototype=o;
    }
    return new F();
}
//相当于是克隆了一个当前对象
//3、构造函数继承
function SuperType(name,age){
    this.name=name;
    this.age=age;
}

function SubType(name,age){
    SuperType.call(this, name)
    this.age=age;
}

let sub = new SubType();
//4、寄生式继承
function createAnother(o){
    var clone = object(o);
        clone.sayName=function(){
            console.log('hi,hi')
        }
        return clone;
}
//用构造包装一层函数，返回这个函数，就成了寄生。

// 5、原型链和构造函数模式
function SuperType(name){
    this.name = name;
}
SuperType.prototype.sayName=function(){
    console.log(this.name);
}
function SubType(name,age){
    SuperType.call(this,name)
    this.age=age;
}

SubType.prototype= new SuperType();
SubType.constructor = SubType;
SubType.prototype.sayAge = function(){
    console.log(this.age)
}

var instance = new SubType('liMei',20);

//缺点是无论什么时候都会创建两次超类型构造函数

//寄生构造函数组合式继承。
function inheritPrototype(subType, superType) {
    var prototype = object(superType.prototype);
    prototype.constructor=subType;
    subType.prototype= prototype;
}

function SuperType(name){
    this.name = name;
    this.color=['123','222']
}

SuperType.prototype.sayName=function(){
    console.log(this.name);
}

function SubType(name,age){
    SupType.call(this,name)
    this.age=age;
}

inheritPrototype(SubType,SuperType)

//Object.assign  Object语法糖
Object.assign({},{123:123})
//


//class语法糖
class SuperType{
    constructor(name){
        this.name=name;
    }
    sayName(){
        console.log(this.name)
    }
}

class SubType extends SuperType{
    constructor(name,age){
        super(name)
        this.age=age;
    }
    sayAge(){
        console.log(this.age)
    }
}



